﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LetiLib.Data;
using LetiLib.Database;
using System.Diagnostics;
using LedCenter.GUI.PopupControls;
using Guifreaks.NavigationBar;

namespace LedCenter.GUI
{
    
    public partial class LedPageControl : UserControl
    {
        public LedPageControl()
        {
            InitializeComponent();

        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        List<LETIPage> pages;

        public List<LETIPage> DataSource
        {
            get {return pages;}
            set
            {
                if (value == null)
                    pages = new List<LETIPage>();
                else
                {
                    pages = value;
                }
                // clean old groups
                //navBarPopup.Bands.Clear();
                //navBarPopup.Controls.Clear();
                popupPanel.Controls.Clear();
                cbPageID.Items.Clear();
                if (pages.Count > 0)
                {
                    foreach (LETIPage p in pages)
                    {
                        cbPageID.Items.Add(p.Header.ID);
                    }
                    cbPageID.SelectedIndex = 0;
                }
            }
        }
        //List<int> images;
        //List<NavBarGroupControlContainer> localContainer = new List<NavBarGroupControlContainer>();
        //List<NavBarGroup> groupLocal = new List<NavBarGroup>();
        public void populatePage(int pageID)
        {
            if (pageID < pages.Count && pageID >= 0)
            {
                // Print page
                pages[pageID].save(pageID + ".page");
                // clean old groups
                //navBarPopup.Bands.Clear();
                //navBarPopup.Controls.Clear();
                popupPanel.Controls.Clear();
                cbEventType.SelectedItem = pages[pageID].Header.EventType;
                lbPageItemCount.Text = "Items Count: " + pages[pageID].Header.ItemCount;
                //localContainer.Clear();
                //groupLocal.Clear();
                int index = 0;
                //navBarPopup.SuspendLayout();
                //List<NaviGroup> groups = new List<NaviGroup>();
                if (pages[pageID].PopUps != null)
                {
                    //popupPanel.BeginInit();//.BeginUpdate();
                    //NaviBand Band = new NaviBand();
                    //Band.Text = "Popups";
                    int yLocation = 0;
                    foreach (POPUP p in pages[pageID].PopUps)
                    {
                        // Create a Local group.
                        // 
                        // naviGroup2
                        // 

                        NaviGroup groupLocal = new NaviGroup(); //groupLocal = new NavBarGroup();
                        //groupLocal.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                        groupLocal.Expanded = false;
                        groupLocal.HeaderContextMenuStrip = null;
                        //groupLocal.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                        //groupLocal.Dock = DockStyle.Top; 
                        groupLocal.Location = new System.Drawing.Point(0, 0);
                        groupLocal.LayoutStyle = Guifreaks.NavigationBar.NaviLayoutStyle.Office2007Blue;
                        groupLocal.Caption = "Pop up: " + p.ID;
                        groupLocal.Name = p.ID.ToString();
                        groupLocal.Padding = new System.Windows.Forms.Padding(1, 22, 1, 1);
                        groupLocal.Size = new System.Drawing.Size(popupPanel.Width, 20);
                        groupLocal.TabIndex = 1;
                        groupLocal.Text = "Pop up: " + p.ID;

                        PopupControl aPopupCtrl = CreateControlFromPopup(p);
                        aPopupCtrl.Width = groupLocal.Width;
                        aPopupCtrl.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                        groupLocal.Controls.Add(aPopupCtrl);
                        groupLocal.ExpandedHeight = aPopupCtrl.Height + groupLocal.HeaderHeight;
                        aPopupCtrl.Location = new Point(0, groupLocal.HeaderHeight);

                        groupLocal.Expanded = false;
                        groupLocal.HeaderMouseClick += new MouseEventHandler(popup_HeaderMouseClick);
                        /*
                        PopupControl aPopupCtrl = CreateControlFromPopup(p);
                        // 
                        // naviGroup1
                        // 
                        groupLocal.Caption = "Pop up: " + p.ID;
                        groupLocal.Dock = System.Windows.Forms.DockStyle.Top;
                        groupLocal.ExpandedHeight = aPopupCtrl.Height + groupLocal.HeaderHeight;
                        //yLocation += groupLocal.ExpandedHeight;
                        groupLocal.HeaderContextMenuStrip = null;
                        groupLocal.LayoutStyle = Guifreaks.NavigationBar.NaviLayoutStyle.Office2007Blue;
                        groupLocal.Location = new System.Drawing.Point(0, yLocation);
                        groupLocal.Name = "Pop up: " + p.ID;
                        groupLocal.Padding = new System.Windows.Forms.Padding(1, 22, 1, 1);
                        //groupLocal.Margin = new System.Windows.Forms.Padding(10, 0, 0, 0);
                        groupLocal.Size = new System.Drawing.Size(221, 110);
                        groupLocal.TabIndex = 21;
                        groupLocal.Text = "Pop up: " + p.ID;
                        aPopupCtrl.Location = new Point(0, groupLocal.HeaderHeight);

                        groupLocal.Controls.Add(aPopupCtrl);

                        aPopupCtrl.Width = popupPanel.Width;
                        //groupLocal.GroupClientHeight = aPopupCtrl.Height + 3; //aPopupCtrl.Dock = DockStyle.Fill;
                        //groupLocal.GroupClientHeight = 120 + 3; //aPopupCtrl.Dock = DockStyle.Fill;
                        groupLocal.Expanded = false;
                         * */
                        popupPanel.Controls.Add(groupLocal);
                        index++;
                    }
                    //groups.Reverse();
                    //groups[0].Expanded = true;
                    //Band.ClientArea.Controls.AddRange(groups.ToArray<Control>());
                    //this.navBarPopup.Bands.Add(Band);
                    //this.navBarPopup.SetActiveBand(Band);
                    //navBarPopup.EndInit();//.EndUpdate();
                    
                }
            }
        }

        private void popupPanel_SizeChanged(object sender, EventArgs e)
        {
           // return; ;
            foreach (Control c in ((Panel)sender).Controls)
            {
                if (c.GetType() == typeof(NaviGroup))
                {
                    c.Width = ((Panel)sender).Width;
                    /*
                    foreach (Control cc in c.Controls)
                    {
                        if (cc.GetType().BaseType == typeof(PopupControl))
                        {
                            ///((NaviGroup)c).ExpandedHeight = cc.Height + ((NaviGroup)c).HeaderHeight;
                            cc.Location = new Point(0, 20);
                        }
                    }
                     * */
                    //c.Size = new System.Drawing.Size(((Panel)sender).Width, 20);
                }
            }
        }
        

        private void popup_HeaderMouseClick(object sender, MouseEventArgs e)
        {
            selectedPopup = (ushort)popupPanel.Controls.IndexOf((Control)sender);
            foreach (Control c in popupPanel.Controls)
            {
                if (c.GetType() == typeof(NaviGroup))
                {
                    if (c.Equals(sender))
                        ((NaviGroup)c).Expanded = true;
                    else
                        ((NaviGroup)c).Expanded = false;
                }
            }
        }

        private PopupControl CreateControlFromPopup(POPUP p)
        {
            PopupControl aPopupCtrl;
            if (p.DataType == POPUP_TYPE.PLAYLIST)
            {
                aPopupCtrl = new PlaylistPopupControl((PLAYLIST_POPUP)p);

                ((PlaylistPopupControl)aPopupCtrl).OnImageUpdate +=
                    new PlaylistPopupControl.UpdateImageHandler(aPopupCtrl_OnImageUpdate);
                ((PlaylistPopupControl)aPopupCtrl).OnImageAdd +=
                    new PlaylistPopupControl.AddImageToPlaylistHandler(aPopupCtrl_OnImageAdd);
                ((PlaylistPopupControl)aPopupCtrl).OnImageDelete +=
                    new PlaylistPopupControl.DeleteImageFromPlaylistHandler(aPopupCtrl_OnImageDelete);
            }
            else if (p.DataType == POPUP_TYPE.TABLE)
            {
                aPopupCtrl = new TablePopupControl((TABLE_POPUP)p);
            }
            else if (p.DataType == POPUP_TYPE.TEXT)
            {
                aPopupCtrl = new TextPopupControl((TEXT_POPUP)p);
            }
            else
                aPopupCtrl = new GeneralPopupControl(p);

            aPopupCtrl.Resize += new EventHandler(aPopupCtrl_Resize);

            aPopupCtrl.OnPopupUpdate += new PopupControl.UpdatePopupHandler(aPopupCtrl_OnPopupUpdate);
            //aPopupCtrl.OnPopupUpdate += new LedPopUpControl.UpdatePopupHandler(aPopupCtrl_OnPopupUpdate);
            
            return aPopupCtrl;
        }

        void aPopupCtrl_Resize(object sender, EventArgs e)
        {
            PopupControl aPopupCtrl = (PopupControl)sender;
            int activePage = int.Parse(cbPageID.Text) - 1;
            int popupIndex = pages[activePage].PopUps.IndexOf(aPopupCtrl._popup);

            //aPopupCtrl.Dock = DockStyle.Fill;
            //navBarPopup.Bands[0].[popupIndex].GroupClientHeight = aPopupCtrl.Height + 3; 
        }

        private void OnLoad(object sender, EventArgs e)
        {
            /*
            pages = XmlDatabase.Instance.getPagesData();
            foreach (LETIPage p in pages)
            {
                cbPageID.Items.Add(p.ID);
            }
            cbPageID.SelectedIndex = 0;
             * */
            //images = new List<int>();
            cbEventType.DataSource = Enum.GetValues(typeof(EVENT_TYPE));
            if (XmlDatabase.Instance.getImageTable() != null)
                cbImageID.DataSource = XmlDatabase.Instance.getImageTable();
            cbImageID.DisplayMember = "ID";
            cbImageID.ValueMember = "ID";
            cbImageID.SelectedIndex = 0;
        }

        private void cbPageID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPageID.Text != "")
                populatePage(int.Parse(cbPageID.Text) - 1);
        }
        ushort selectedPopup = 0;
        /*
        private void navBarPopup_GroupExpanded(object sender, NavBarGroupEventArgs e)
        {
            selectedPopup = (ushort)navBarPopup.Groups.IndexOf(e.Group);
            foreach( NavBarGroup nbg in navBarPopup.Groups)
            {
                if (nbg.Name != e.Group.Name) 
                    nbg.Expanded = false ;
            }
        }
         * */

        #region | Page control |
        public delegate void PageActiveHandler(ushort pageID);
        public event PageActiveHandler ActivePage;
        private void btnActivePage_Click(object sender, EventArgs e)
        {
            if (cbPageID.Items.Count <= 0)
            {
                MessageBox.Show("There is no page!");
                Debug.WriteLine("There is no page!"); 
                return;
            }
            ushort pageID = (ushort)cbPageID.SelectedIndex;
            pageID = pages[pageID].Header.ID;
            ActivePage(pageID);
        }

        public delegate void CreatePageHandler(LETIPage page);
        public event CreatePageHandler OnPageCreated;
        private void btnCreate_Click(object sender, EventArgs e)
        {
            ushort pageID = 0;
            LETIPage page = null;

            bool foundID = false;
            for (pageID = 0; pageID < pages.Count; pageID++)
            {
                foreach (LETIPage p in pages)
                {
                    if (p.Header.ID == pageID + 1)
                    {
                        foundID = true;
                        break;
                    }
                    foundID = false;
                }
                if (foundID)
                    continue;
                break;
            }
            cbPageID.Items.Add(++pageID);
            page = new LETIPage(pageID);
            OnPageCreated(page);
        }

        public delegate void UpdatePageHeaderHandler(ushort pageID,LETIPageHeader pageHeader);
        public event UpdatePageHeaderHandler OnPageHeaderUpdate;
        private void btnUpdatePage_Click(object sender, EventArgs e)
        {
            btnUpdateOK.Enabled = btnUpdateCancel.Enabled = true;
            btnUpdateOK.Visible = btnUpdateCancel.Visible = true;
            cbEventType.Enabled = true;
            btnUpdatePage.Enabled = false;
        }
        private void btnUpdateOK_Click(object sender, EventArgs e)
        {
            ushort pageID = (ushort)cbPageID.SelectedIndex;
            if (pages[pageID] != null)
            {
                pages[pageID].Header.EventType = ((EVENT_TYPE)Enum.Parse(typeof(EVENT_TYPE), cbEventType.SelectedItem.ToString()));
                btnUpdateOK.Enabled = btnUpdateCancel.Enabled = false;
                btnUpdateOK.Visible = btnUpdateCancel.Visible = false;
                cbEventType.Enabled = false;
                btnUpdatePage.Enabled = true;

                if (OnPageHeaderUpdate != null)
                {
                    OnPageHeaderUpdate(pages[pageID].Header.ID, pages[pageID].Header);
                }
            }
        }

        private void btnUpdateCancel_Click(object sender, EventArgs e)
        {
            ushort pageID = (ushort)cbPageID.SelectedIndex;
            if (pages[pageID] != null)
            {
                cbEventType.SelectedItem = pages[pageID].Header.EventType;
                btnUpdateOK.Enabled = btnUpdateCancel.Enabled = false;
                btnUpdateOK.Visible = btnUpdateCancel.Visible = false;
                cbEventType.Enabled = false;

                btnUpdatePage.Enabled = true;
            }
        }

        public delegate void DeletePageHandler(ushort pageID);
        public event DeletePageHandler OnPageDeleted;
        private void btnDeletePage_Click(object sender, EventArgs e)
        {
            ushort pageID = ushort.Parse(cbPageID.Text.ToString());

            OnPageDeleted(pageID);
            cbPageID.Items.RemoveAt(pageID); 
        }
        #endregion

        #region | Image data control |
        public delegate void ImageActiveHandler(ushort pageID, ushort popupID, ushort imgID);
        public event ImageActiveHandler ActiveImage;
        private void btnActiveImgage_Click(object sender, EventArgs e)
        {
            ushort pageID = (ushort)cbPageID.SelectedIndex;

            ushort popupID = 0;
            if (pages[pageID].PopUps != null)
            {
                foreach (POPUP p in pages[pageID].PopUps)
                {
                    popupID++;
                    if (p.Z == 0)
                        break;
                }
            }
            ushort imgID = (((XmlDatabase.IMAGEDATA)(cbImageID.SelectedItem)).ID);
            pageID = pages[pageID].Header.ID;
            if (ActiveImage != null)
                ActiveImage(pageID, popupID, imgID);
        }

        public delegate void DeleteImageHandler(ushort imgageID);
        public event DeleteImageHandler OnDeleteImage;
        private void btnDelImgage_Click(object sender, EventArgs e)
        {
            ushort imgID = (ushort)(cbImageID.SelectedIndex + 1);
            if (OnDeleteImage != null)
                OnDeleteImage(imgID);
        }

        public delegate void DeleteAllImageHandler();
        public event DeleteAllImageHandler OnDeleteAllImage;
        private void btnDelAllImgage_Click(object sender, EventArgs e)
        {
            if (OnDeleteAllImage != null)
                OnDeleteAllImage();
        }

        public delegate void SetPredefineHandler(ushort imgageID, byte romID);
        public event SetPredefineHandler OnSetPredefine;
        private void btnSetPredefine_Click(object sender, EventArgs e)
        {
            byte romID = 0;
            string input = Microsoft.VisualBasic.Interaction.InputBox("Choose ROM ID", "ROMID chooser", "1");
            try
            {
                romID = byte.Parse(input);
                if (romID < 0 || romID > 4)
                {
                    MessageBox.Show("Rom ID not valid! ( 0 < RomID < 5)");
                    return;
                }
                ushort imgID = (ushort)(cbImageID.SelectedIndex + 1);
                if (OnSetPredefine != null)
                    OnSetPredefine(imgID, romID);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Rom ID not valid! ( 0 < RomID < 4)");
            }
            
        }
        #endregion

        #region | System control |
        public delegate void GetBrightHandler();
        public event GetBrightHandler OnGetBright;
        private void btnGetBright_Click(object sender, EventArgs e)
        {
            if (OnGetBright != null)
            OnGetBright();
        }

        public delegate void GetVersionHandler();
        public event GetVersionHandler OnGetVersion;
        public delegate void GetStatusHandler();
        public event GetStatusHandler OnGetStatus;
        private void btnGetFirmware_Click(object sender, EventArgs e)
        {
            if (OnGetStatus != null)
                OnGetStatus();
        }

        public delegate void CheckLedHandler();
        public event CheckLedHandler OnCheckLed;
        private void btnCheckLed_Click(object sender, EventArgs e)
        {
            if (OnCheckLed != null)
                OnCheckLed();
        }

        public delegate void GetTimeHandler();
        public event GetTimeHandler OnGetTime;
        private void btnSettime_Click(object sender, EventArgs e)
        {
            if (OnGetTime != null)
            OnGetTime();
        }

        public delegate void GetActivePageHandler();
        public event GetActivePageHandler OnGetActivePage;
        private void btnGetActivePage_Click(object sender, EventArgs e)
        {
            if (OnGetActivePage != null)
            OnGetActivePage();
        }

        public delegate void GetPageInfoHandler(ushort pageID);
        public event GetPageInfoHandler OnGetPageInfo;
        private void btnGetPageInfo_Click(object sender, EventArgs e)
        {
            ushort pageID = 0;
            string input = Microsoft.VisualBasic.Interaction.InputBox("Choose page ID", "Page chooser", "1");
            try
            {
                pageID = ushort.Parse(input);
                if (OnGetPageInfo != null)
                    OnGetPageInfo(pageID);
            }
            catch (System.Exception ex)
            {

            }

        }

        public delegate void GetImageListHandler();
        public event GetImageListHandler OnGetImageList;
        private void btnGetImageList_Click(object sender, EventArgs e)
        {
            if (OnGetImageList != null)
            {
                OnGetImageList();
            }
        }

        public delegate void CaptureScreenHandler();
        public event CaptureScreenHandler OnCaptureScreen;
        private void btnCapture_Click(object sender, EventArgs e)
        {
            if (OnCaptureScreen != null)
            {
                OnCaptureScreen();
            }
        }

        public delegate void RebootHandler();
        public event RebootHandler OnReboot;
        private void btnReboot_Click(object sender, EventArgs e)
        {
            if (OnReboot != null)
            {
                OnReboot();
            }
        }

        public delegate void ClearScreenHandler();
        public event ClearScreenHandler OnClearScreen;
        private void btnClear_Click(object sender, EventArgs e)
        {
            if (OnClearScreen != null)
            {
                OnClearScreen();
            }
        }

        public delegate void ScanScreenHandler();
        public event ScanScreenHandler OnScanScreen;
        private void btnScanLed_Click(object sender, EventArgs e)
        {
            if (OnScanScreen != null)
            {
                OnScanScreen();
            }
        }
        #endregion

        #region | Popup control |
        public delegate void AddPopupHandler(ushort pageID,ushort popupID,POPUP popupData);
        public event AddPopupHandler OnPopupAdded;
        private void btnAddPopup_Click(object sender, EventArgs e)
        {
            ushort pageID = ushort.Parse(cbPageID.Text);
            
            POPUP p ;
            Form form1 = new Form();
            GeneralPopupControl popupControl = new GeneralPopupControl(new GENERAL_POPUP(), true);
            
            form1.Controls.Add(popupControl);
            
            popupControl.Dock = DockStyle.Fill;
            form1.Size = popupControl.Size;// new Size(popupControl.Size.Width + 5, popupControl.Size.Height + 30);
            DialogResult dr = form1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //ushort popupID = (ushort)(pages[pageID - 1].Header.ItemCount + 1);
                ushort popupID = 0;
                popupID = getAvaibleIDInList(pages[pageID - 1].PopUps);
                p = popupControl._popup;
                p.ID = popupID;
                pages[pageID - 1].AddPopup(p);
                
                //navBarPopup.BeginUpdate();
                // Create a Local group.
                NaviGroup groupLocal = new NaviGroup();

                // Create popup control
                PopupControl aPopupCtrl = CreateControlFromPopup(p);
                        
                // Add to navigation bar group
                //this.navBarPopup.Bands[0].Controls.Add(groupLocal);
                this.popupPanel.Controls.Add(groupLocal);
                // 
                // navBarGroup1
                // 
                groupLocal.Caption = "Pop up: " + (p.ID);
                //groupLocal.ControlContainer = new NavBarGroupControlContainer();
                groupLocal.Controls.Add(aPopupCtrl);
                groupLocal.LayoutStyle = Guifreaks.NavigationBar.NaviLayoutStyle.Office2007Blue;
                groupLocal.Name = "Pop up:" + p.ID;

                aPopupCtrl.Width = popupPanel.Width;
                //groupLocal.GroupClientHeight = aPopupCtrl.Height + 3; //aPopupCtrl.Dock = DockStyle.Fill;
                //groupLocal.GroupClientHeight = 120 + 3; //aPopupCtrl.Dock = DockStyle.Fill;
                groupLocal.Expanded = false;
                //navBarPopup.EndUpdate();

                if (OnPopupAdded != null)
                    OnPopupAdded(pageID, popupID, p);

                pages[pageID - 1].Header.ItemCount++;
                lbPageItemCount.Text = "Items Count: " + pages[pageID - 1].Header.ItemCount;
                this.Validate(true);
            }
        }

        private ushort getAvaibleIDInList(List<POPUP> list)
        {
            if (list == null)
                return 1;
            ushort i = 1;
            bool flag = false;
            for ( i = 1; i < list.Count+1; i++)
            {
                flag = false;
                foreach (POPUP p in list)
                {
                    if (i == p.ID)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                    return (ushort)(i);

            }
            return (ushort)(i);
        }

        public delegate void DeletePopupHandler(ushort pageID, ushort popupID);
        public event DeletePopupHandler OnPopupDeleted;
        private void btnDeletePopup_Click(object sender, EventArgs e)
        {
            ushort pageID = (ushort)cbPageID.SelectedIndex;
            ushort popupID = pages[pageID].PopUps[selectedPopup].ID;

            pages[pageID].Header.ItemCount--;
            pages[pageID].PopUps.RemoveAt(selectedPopup);
            lbPageItemCount.Text = "Items Count: " + pages[pageID].Header.ItemCount;
            //navBarPopup.BeginUpdate();
            //popupPanel.Controls.RemoveAt(selectedPopup);
            int index = 0;
            foreach (Control c in popupPanel.Controls)
            {
                if (c.GetType() == typeof(NaviGroup))
                {
                    if (index == selectedPopup)
                        popupPanel.Controls.Remove(c);
                    index++;
                }
            }

            //navBarPopup.EndUpdate();
            if (OnPopupDeleted != null)
                OnPopupDeleted(pages[pageID].Header.ID, popupID);
        }

        public delegate void UpdatePopupHandler(ushort pageID, ushort popupID, POPUP popupData);
        public event UpdatePopupHandler OnPopupUpdate;
        void aPopupCtrl_OnPopupUpdate(object sender)
        {
            PopupControl pControl = (PopupControl)sender;
            ushort pageID = ushort.Parse(cbPageID.Text);
            ushort popupID = pControl._popup.ID;
            POPUP p = ((PopupControl)sender)._popup;

            //p.ID = popupID;

            if (OnPopupUpdate != null)
                OnPopupUpdate(pageID, p.ID, p);

        }

        public delegate void AddImageToPlaylistHandler(ushort pageID, ushort popupID, IMAGE imageData);
        public event AddImageToPlaylistHandler OnImageAdd;
        void aPopupCtrl_OnImageAdd(object sender)
        {
            PopupControl pControl = (PopupControl)sender;
            ushort pageID = ushort.Parse(cbPageID.Text);
            //ushort popupID = pControl._popup;
            PLAYLIST_POPUP p = (PLAYLIST_POPUP)((PopupControl)sender)._popup;

            //p.ID = popupID;

            if (OnImageAdd != null)
                OnImageAdd(pageID, p.ID, p.Images[p.Images.Count-1]);
        }

        public delegate void UpdateImageHandler(ushort pageID, ushort popupID, byte playOrder, IMAGE imageData);
        public event UpdateImageHandler OnImageUpdate;
        void aPopupCtrl_OnImageUpdate(object sender, byte playOrder)
        {
            PopupControl pControl = (PopupControl)sender;
            ushort pageID = ushort.Parse(cbPageID.Text);
            //ushort popupID = pControl._popup;
            PLAYLIST_POPUP p = (PLAYLIST_POPUP)((PopupControl)sender)._popup;

            //p.ID = popupID;

            if (OnImageUpdate != null)
                OnImageUpdate(pageID, p.ID, playOrder, p.Images[playOrder]);

        }

        public delegate void DeleteImageFromPlaylistHandler(ushort pageID, ushort popupID, byte playOrder);
        public event DeleteImageFromPlaylistHandler OnImageDelete;
        void aPopupCtrl_OnImageDelete(object sender,byte playOrder)
        {
            PopupControl pControl = (PopupControl)sender;
            ushort pageID = ushort.Parse(cbPageID.Text);
            //ushort popupID = pControl._popup;
            PLAYLIST_POPUP p = (PLAYLIST_POPUP)((PopupControl)sender)._popup;

            //p.ID = popupID;

            if (OnImageDelete != null)
                OnImageDelete(pageID, p.ID, playOrder);
        }
        #endregion
       
    }
}
